package org.tmsps.ne4spring.utils.http;

import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.tmsps.ne4spring.utils.JsonUtil;

public class HttpBodyTools {

	public static String post(String url, Map<?, ?> paramsMap) {
		return post(url, JsonUtil.toJson(paramsMap));
	}

	public static String post(String url, String params) {
		System.err.println(url);
		CloseableHttpClient client = HttpClients.createDefault();

		String strPostResponseBody = "";
		HttpPost post = new HttpPost(url);
		// post.addHeader("Content-Type", "application/json;charset=utf-8");

		try {
			StringEntity se = new StringEntity(params, "UTF-8");
			post.setEntity(se);
			HttpResponse response = client.execute(post);
			HttpEntity entity = response.getEntity();
			if (null != entity) {
				strPostResponseBody = EntityUtils.toString(entity);
				EntityUtils.consume(entity);
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			// try {
			// client.close();
			// } catch (Exception e) {
			// e.printStackTrace();
			// }
		}

		System.err.println(strPostResponseBody);
		return strPostResponseBody;

	}

}
